/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.cib.fintech.entrustedinvestment.biz.service.impl;

import com.cib.fintech.entrustedinvestment.biz.domain.ProdInfoExtra;
import com.cib.fintech.entrustedinvestment.utils.ValidationUtil;
import com.cib.fintech.entrustedinvestment.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.cib.fintech.entrustedinvestment.biz.repository.ProdInfoExtraRepository;
import com.cib.fintech.entrustedinvestment.biz.service.ProdInfoExtraService;
import com.cib.fintech.entrustedinvestment.biz.service.dto.ProdInfoExtraDto;
import com.cib.fintech.entrustedinvestment.biz.service.dto.ProdInfoExtraQueryCriteria;
import com.cib.fintech.entrustedinvestment.biz.service.mapstruct.ProdInfoExtraMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.cib.fintech.entrustedinvestment.utils.PageUtil;
import com.cib.fintech.entrustedinvestment.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zhangqingkun
* @date 2020-10-15
**/
@Service
@RequiredArgsConstructor
public class ProdInfoExtraServiceImpl implements ProdInfoExtraService {

    private final ProdInfoExtraRepository prodInfoExtraRepository;
    private final ProdInfoExtraMapper prodInfoExtraMapper;

    @Override
    public Map<String,Object> queryAll(ProdInfoExtraQueryCriteria criteria, Pageable pageable){
        Page<ProdInfoExtra> page = prodInfoExtraRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(prodInfoExtraMapper::toDto));
    }

    @Override
    public List<ProdInfoExtraDto> queryAll(ProdInfoExtraQueryCriteria criteria){
        return prodInfoExtraMapper.toDto(prodInfoExtraRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ProdInfoExtraDto findById(String prodCode) {
        ProdInfoExtra prodInfoExtra = prodInfoExtraRepository.findById(prodCode).orElseGet(ProdInfoExtra::new);
        ValidationUtil.isNull(prodInfoExtra.getProdCode(),"ProdInfoExtra","prodCode",prodCode);
        return prodInfoExtraMapper.toDto(prodInfoExtra);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ProdInfoExtraDto create(ProdInfoExtra resources) {
        resources.setProdCode(IdUtil.simpleUUID()); 
        return prodInfoExtraMapper.toDto(prodInfoExtraRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProdInfoExtra resources) {
        ProdInfoExtra prodInfoExtra = prodInfoExtraRepository.findById(resources.getProdCode()).orElseGet(ProdInfoExtra::new);
        ValidationUtil.isNull( prodInfoExtra.getProdCode(),"ProdInfoExtra","id",resources.getProdCode());
        prodInfoExtra.copy(resources);
        prodInfoExtraRepository.save(prodInfoExtra);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String prodCode : ids) {
            prodInfoExtraRepository.deleteById(prodCode);
        }
    }

    @Override
    public void download(List<ProdInfoExtraDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProdInfoExtraDto prodInfoExtra : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("产品名称（SAM推送）", prodInfoExtra.getProdName());
            map.put("清算速度", prodInfoExtra.getClearType());
            map.put("资管计划属性：01集合，04定向，05专户", prodInfoExtra.getProperty());
            map.put("资产分类 01固定收益类", prodInfoExtra.getAssertClassify());
            map.put("暂停交易业务（9926违约退出9931申购撤单9925预约赎回9927实时赎回9920认购9921申购9932赎回撤单9922预约申购9924赎回9930认购撤单默认为空为全部允许000-为全部暂停相当于暂停交易）", prodInfoExtra.getInfo1());
            map.put("是否由金融资产投资公司发行0否1是", prodInfoExtra.getIsIssued());
            map.put("资管计划发起人机构编码", prodInfoExtra.getOrgCode());
            map.put("资管计划登记编码", prodInfoExtra.getRegisterCode());
            map.put("管理人", prodInfoExtra.getManager());
            map.put("托管人", prodInfoExtra.getTrustee());
            map.put("发行金额", prodInfoExtra.getIssueAmount());
            map.put("资金实际投向 20固定收益类、21权益类、22商品及金融衍生品类、23混合类\r\n根据合作业务确定，一般为20固定收益类\r\n", prodInfoExtra.getInvestTo());
            map.put("资金运用方式", prodInfoExtra.getFundApplicationMode());
            map.put("资金运用行业", prodInfoExtra.getFundApplicationIndustry());
            map.put("资管计划成立日期", prodInfoExtra.getEstablishDate());
            map.put("资管计划终止日期", prodInfoExtra.getExpiryDate());
            map.put("资管计划属性 一般为01集合", prodInfoExtra.getPlanMode());
            map.put("购买结构01优先02劣后03整体\r\n根据合作业务确定，一般为03整体\r\n", prodInfoExtra.getBuyStructure());
            map.put("管理方式根据合作业务确定一般为02被动委托管理", prodInfoExtra.getManageStyle());
            map.put("管理费率", prodInfoExtra.getManageFee());
            map.put("托管费率", prodInfoExtra.getCustodianFee());
            map.put("中介服务机构合计费率", prodInfoExtra.getServiceFee());
            map.put("其他合计费率", prodInfoExtra.getOtherFee());
            map.put("备注", prodInfoExtra.getRemark());
            map.put("创建时间", prodInfoExtra.getCreateTime());
            map.put("更新时间", prodInfoExtra.getUpdateTime());
            map.put("预期最高收益率%", prodInfoExtra.getExpectTopYieid());
            map.put("预期最低收益率%", prodInfoExtra.getExpectMinYieid());
            map.put("资管计划属性 一般为01集合", prodInfoExtra.getHasYieid());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}